from django.db import models

class User(models.Model):
    """
    用户表
    """
    user_id = models.BigAutoField(primary_key=True, db_comment='用户ID')
    username = models.CharField(unique=True, max_length=50, db_comment='用户名')
    password = models.CharField(max_length=255, db_comment='密码')
    avatar_url = models.CharField(max_length=255, blank=True, null=True, db_comment='头像logo URL')
    phone = models.CharField(max_length=20, blank=True, null=True, db_comment='主要联系电话')
    email = models.CharField(max_length=100, blank=True, null=True, db_comment='主要邮箱')
    register_time = models.DateTimeField(db_comment='注册时间', auto_now_add=True)
    last_login_time = models.DateTimeField(blank=True, null=True, db_comment='最后登录时间')
    status = models.IntegerField(db_comment='用户状态(1:激活, 0:禁用)', default=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'user'
        db_table_comment = '用户表'
        
    def __str__(self):
        return self.username


class ParkEmployeeProfile(models.Model):
    profile_id = models.BigAutoField(primary_key=True, db_comment='档案ID')
    user = models.OneToOneField(User, models.DO_NOTHING, db_comment='关联用户ID')
    name = models.CharField(max_length=50, db_comment='姓名')
    gender = models.CharField(max_length=1, blank=True, null=True, db_comment='性别(M/F)')
    id_card = models.CharField(max_length=18, blank=True, null=True, db_comment='身份证号')
    employee_no = models.CharField(max_length=50, db_comment='工号')
    department = models.CharField(max_length=50, blank=True, null=True, db_comment='所属部门')
    employee_type = models.CharField(max_length=20, db_comment='员工类型(管理员/保洁工/维修工等)')
    hire_date = models.DateField(db_comment='入职日期')
    address = models.TextField(blank=True, null=True, db_comment='联系地址')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'park_employee_profile'
        db_table_comment = '园区员工档案表'


class Company(models.Model):
    """
    企业表
    """
    company_id = models.BigAutoField(primary_key=True, db_comment='企业ID')
    company_type = models.CharField(max_length=50, db_comment='企业类型')
    company_name = models.CharField(max_length=100, db_comment='企业名称')
    credit_code = models.CharField(unique=True, max_length=50, db_comment='统一社会信用代码')
    legal_representative_user = models.ForeignKey(User, models.DO_NOTHING, blank=True, null=True, db_comment='法人代表用户ID')
    contact_phone = models.CharField(max_length=20, blank=True, null=True, db_comment='联系电话')
    entry_date = models.DateField(db_comment='入驻日期')
    description = models.TextField(blank=True, null=True, db_comment='描述')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'company'
        db_table_comment = '企业表'
        
    def __str__(self):
        return self.company_name


class CompanyEmployeeProfile(models.Model):
    profile_id = models.BigAutoField(primary_key=True, db_comment='档案ID')
    user = models.OneToOneField(User, models.DO_NOTHING, db_comment='关联用户ID')
    company = models.ForeignKey(Company, models.DO_NOTHING, db_comment='所属企业ID')
    name = models.CharField(max_length=50, db_comment='姓名')
    gender = models.CharField(max_length=1, blank=True, null=True, db_comment='性别(M/F)')
    id_card = models.CharField(max_length=18, blank=True, null=True, db_comment='身份证号')
    employee_no = models.CharField(max_length=50, blank=True, null=True, db_comment='工号(企业内)')
    department = models.CharField(max_length=50, blank=True, null=True, db_comment='所属部门(企业内)')
    position = models.CharField(max_length=50, blank=True, null=True, db_comment='职位')
    is_legal_representative = models.IntegerField(db_comment='是否为法人代表')
    is_department_manager = models.IntegerField(db_comment='是否是部门经理')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'company_employee_profile'
        db_table_comment = '企业员工档案表'


class Department(models.Model):
    department_id = models.BigAutoField(primary_key=True, db_comment='部门ID')
    department_name = models.CharField(max_length=50, db_comment='部门名称')
    department_code = models.CharField(max_length=50, blank=True, null=True, db_comment='部门编码')
    company = models.ForeignKey(Company, models.DO_NOTHING, db_comment='所属企业ID')
    manager_user = models.ForeignKey(User, models.DO_NOTHING, blank=True, null=True, db_comment='负责人用户ID')
    description = models.TextField(blank=True, null=True, db_comment='描述/职责说明')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'department'
        db_table_comment = '部门表'


class Visitor(models.Model):
    """
    访客档案表
    """
    visitor_id = models.BigAutoField(primary_key=True, db_comment='访客ID')
    name = models.CharField(max_length=50, db_comment='姓名')
    gender = models.CharField(max_length=1, blank=True, null=True, db_comment='性别(M/F)')
    id_card = models.CharField(unique=True, max_length=18, blank=True, null=True, db_comment='身份证号')
    phone = models.CharField(unique=True, max_length=20, db_comment='联系电话')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'visitor'
        db_table_comment = '访客档案表'
        
    def __str__(self):
        return self.name
